<?php

namespace app\mall_back\controller;

use app\data\model\DataUser;
use app\mall\logic\orderLogic;
use app\mall_back\model\GoodsLuckyDrawModel;
use app\mall_back\model\GoodsModel;
use think\admin\Controller;
use think\facade\Event;

class GoodsLuckyDraw extends Controller
{

    /**
     * 商品抽奖管理
     *
     * @auth true
     */
    public function index()
    {
        $this->title = '商品抽奖管理';
        $query       = GoodsLuckyDrawModel::mQuery();
        $query->order('create_time DESC, sequence_current DESC, id desc')->page();
    }

    /**
     * 添加抽奖数据
     *
     * @auth true
     */
    public function add()
    {
        $this->mode  = 'add';
        $this->title = '添加抽奖数据';
        // 加载对应数据
        GoodsLuckyDrawModel::mForm('form');
    }

    /**
     * 编辑抽奖数据
     *
     * @auth true
     */
    public function edit()
    {
        $this->mode  = 'edit';
        $this->title = '编辑抽奖数据';

        $id = $this->request->param('id', 0);
        $draw = GoodsLuckyDrawModel::find($id);

        $this->choice_robots = DataUser::where('is_bot', 1)
            ->whereIn('id', explode(',', $draw->choice_robots))
            ->fieldRaw('id AS value, nickname AS `name`')
            ->select();

        GoodsLuckyDrawModel::mForm('form');
    }

    /**
     * 开奖
     *
     * @auth true
     */
    public function run_lottery()
    {
        $map = $this->_vali(['id.require' => 'ID不能为空！']);
        $res = orderLogic::run_lottery($map['id']);
        if ($res['code'] == 200) $this->success($res['msg']);
        $this->error($res['msg'], '', $res['code']);

    }

    //数据列表处理
    protected function _page_filter(array &$data)
    {
        foreach ($data as $k => &$v) {
            $goods           = GoodsModel::where('id', '=', $v['goods_id'])
                ->field('code ,goods_name ,cover')
                ->find();
            $v['code']       = $goods['code'];
            $v['goods_name'] = $goods['goods_name'];
            $v['cover']      = $goods['cover'];
        }

    }

    //表单数据处理
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            $this->goods = GoodsModel::where([
                ['status', '=', 1],
                ['deleted', '=', 0],
            ])->select();
        }

        if ($this->request->isPost()) {
            $act        = $this->request->has('id') ? 'edit' : 'add';

            if ($act == 'add') {
                $pn                     = $data['participants_number'] ?? 0;
                $data['residue_number'] = $data['people_number'] - $pn;
                $data['sequence_no']    = '';
                if (isset($data['sequence_total']) && $data['sequence_total'] > 1) {
                    $data['sequence_no'] = 'M' . date('YmdHis');
                }
            }

            if ($act == 'edit') {

            }
        }
    }

    /**
     * @param bool $result
     * @param      $edata
     *
     * @return void
     */
    protected function _form_result(bool $result, $edata = NULL)
    {
        $exe_result = TRUE;
        $act        = $this->request->has('id') ? 'edit' : 'add';
        if ($result && $act == 'add') { // 新建成功时
            if ($edata['sequence_total'] > 1) { // 多期
                $exe_result &= GoodsLuckyDrawModel::createSubLuckyDraw($edata);
            }

            if ($edata['participants_number'] > 0 && !empty($edata['choice_robots'])) {
                Event::trigger('RobotsFilling', $edata);
            }
        }

        return $exe_result;
    }
}